import cv2 as cv 
import numpy as np 
import matplotlib.pyplot as plt 

img_flower = cv.imread('img/2.jpg',0)
img_man_1 = cv.imread('img/1.jpg',0)

plt.imshow(img_man_1,'gray')
plt.show()

height,width=img_flower.shape[:2]

img_man = cv.resize(img_man_1,(width,height))

plt.imshow(img_man,'gray')
plt.show()

plt.subplot(221),plt.imshow(img_flower,'gray'),plt.title('origial1')
plt.xticks([]),plt.yticks([])
plt.subplot(222),plt.imshow(img_man,'gray'),plt.title('origial_2')
plt.xticks([]),plt.yticks([])

f1 = np.fft.fft2(img_flower)
f1shift=np.fft.fftshift(f1)
f1_A = np.abs(f1shift)
f1_P =np.angle(f1shift)


f2=np.fft.fft2(img_flower)
f2shift=np.fft.fftshift(f2)
f2_A=np.abs(f2shift)
f2_P=np.angle(f2shift)

img_new1_f=np.zeros(img_flower.shape,dtype=complex)
img1_real=f1_A*np.cos(f2_P)
img1_imag=f1_A*np.sin(f2_P)
img_new1_f.real=np.array(img1_real)
img_new1_f.imag=np.array(img1_imag)
f3shift=np.fft.ifft2(img_new1_f)
img_new1=np.fft.ifft2(f3shift)
img_new1=np.abs(img_new1)

img_new1=(img_new1-np.amin(img_new1))/(np.amax(img_new1)-np.amin(img_new1))
plt.subplot(223),plt.imshow(img_new1,'gray'),plt.title('another way')
plt.xticks([]),plt.yticks([])
img_new2_f=np.zeros(img_flower.shape,dtype=complex)
img2_real=f2_A*np.cos(f1_P)
img2_imag=f2_A*np.sin(f1_P)
img_new2_f.real=np.array(img2_real)
img_new2_f.imag=np.array(img2_imag)
f4shift=np.fft.ifftshift(img_new2_f)
img_new2=np.fft.ifft2(f4shift)
img_new2=np.abs(img_new2)

img_new2=(img_new2-np.amin(img_new2))/(np.amax(img_new2)-np.amin(img_new2))
plt.xticks([]),plt.yticks([])
plt.show()